package com.example.control;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.domain.ConbinatinPojo;
import com.example.domain.User;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class HelloControl {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/hello")
    public String hello() {
        User user = userService.getUserById("1");
        return user.toString();
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);

            // 设置EXCEL名称
            String fileName = new String(("SystemExcel").getBytes(), "UTF-8");

            // 设置SHEET名称
            Sheet sheet = new Sheet(1, 0);
            sheet.setSheetName("系统列表sheet1");

            // 设置标题
            Table table = new Table(1);
            List<List<String>> titles = new ArrayList<List<String>>();
            titles.add(Arrays.asList("ID"));
            titles.add(Arrays.asList("LNAME"));
            titles.add(Arrays.asList("SORT"));
            titles.add(Arrays.asList("LID"));
            titles.add(Arrays.asList("RNAME"));
            titles.add(Arrays.asList("RSORT"));
            titles.add(Arrays.asList("RID"));
            table.setHead(titles);

            // 查询总数并 【封装相关变量 这块直接拷贝就行 不要改动】
            // Integer totalRowCount = userService.selectCountSysSystemVOList();
            Integer totalRowCount = 1000000;
            Integer pageSize = 2000;
            Integer writeCount = totalRowCount % pageSize == 0 ? (totalRowCount / pageSize) : (totalRowCount / pageSize + 1);

            // 写数据 这个i的最大值直接拷贝就行了 不要改
            for (int i = 0; i < writeCount; i++) {
                List<List<String>> dataList = new ArrayList<>();

                // 此处查询并封装数据即可 currentPage, pageSize这个变量封装好的 不要改动
                // PageHelper.startPage(i + 1, pageSize);
                // pageNum
                List<ConbinatinPojo> sysSystemVOList = userService.selectSysSystemVOList(i + 1, pageSize);
                if (!CollectionUtils.isEmpty(sysSystemVOList)) {
                    sysSystemVOList.forEach(eachSysSystemVO -> {
                        dataList.add(Arrays.asList(
                                eachSysSystemVO.getId(),
                                eachSysSystemVO.getLname(),
                                eachSysSystemVO.getLsort(),
                                eachSysSystemVO.getLid(),
                                eachSysSystemVO.getRname(),
                                eachSysSystemVO.getRsort(),
                                eachSysSystemVO.getRid()
                        ));
                    });
                }
                writer.write0(dataList, sheet, table);
            }

            // 下载EXCEL
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            writer.finish();
            out.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
